//
//  TKYProgressDetailsController.m
//  ProgressReportProject
//
//  Created by apple on 2018/4/24.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYProgressDetailsController.h"
#import "ImageBrowserViewController.h"
#import "TKYDocListModel.h"
#import "TKYRemarkImageCell.h"
#import "TKYProgressDetailCell.h"
#define KProgressDetailCellID @"TKYProgressDetailCell"
@interface TKYProgressDetailsController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableV;

@property (nonatomic, strong)UIView * tableFootView;

//图片显示视图
@property (nonatomic, strong)UICollectionView *collectionView;

/**
 图片model数组
 */
@property (nonatomic, strong)NSArray <TKYDocListModel *>*docListModelS;

@end

@implementation TKYProgressDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置界面
    [self setupUI];
    
    [self requestProgressReportInfoData];
}

- (void)setupUI{
    self.navigationItem.title = @"进度详情";
    [self.view addSubview:self.tableV];
}

/**
 请求详情数据
 */
- (void)requestProgressReportInfoData{
    NSString *url = [NSString stringWithFormat:@"%@%@id=%@&token=%@",kProgressApiBaseUrl, kProgressReportInfoUrl,self.progressModel.ID,[[NSUserDefaults standardUserDefaults]objectForKey:kToken]];
    [KHttpRequestTool GET:url parameters:nil success:^(id response) {
        if ([response[@"code"] integerValue] == 0) {
            self.docListModelS = [TKYDocListModel mj_objectArrayWithKeyValuesArray:response[@"docList"]];
        }else{
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:response[@"msg"]];
        }
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:@"网络错误,请稍候再试"];
    }];
}
#pragma mark-- UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TKYProgressDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:KProgressDetailCellID];
    switch (indexPath.row) {
        case 0:
            {
                cell.titleLabel.text = @"分部分项:";
                cell.contentLabel.text = self.progressModel.flowName;
                cell.progressView.hidden = YES;
                cell.progressLabel.hidden = YES;
                cell.textView.hidden = YES;
            }
            break;
        case 1:
        {
            cell.titleLabel.text = @"事件类型:";
            cell.contentLabel.text = @"进度";
            cell.progressView.hidden = YES;
            cell.progressLabel.hidden = YES;
            cell.textView.hidden = YES;

        }
            break;
        case 2:
        {
            cell.titleLabel.text = @"当前工序:";
            cell.contentLabel.text = self.progressModel.procedureName;
            cell.progressView.hidden = YES;
            cell.progressLabel.hidden = YES;
            cell.textView.hidden = YES;

        }
            break;
        case 3:
        {
            cell.titleLabel.text = @"工序时间:";
            cell.contentLabel.text = self.progressModel.endDate;
            cell.progressView.hidden = YES;
            cell.progressLabel.hidden = YES;
            cell.textView.hidden = YES;

        }
            break;
        case 4:
        {
            cell.titleLabel.text = @"当前进度:";
            cell.contentLabel.hidden = YES;
            cell.textView.hidden = YES;
            cell.progressView.progress = self.progressModel.percent*0.01;
            cell.progressLabel.text = [NSString stringWithFormat:@"%ld%%",self.progressModel.percent];
        }
            break;
        case 5:
        {
            cell.titleLabel.text = @"备注信息:";
            cell.textView.text = self.progressModel.remark;
            cell.contentLabel.hidden = YES;
            cell.progressView.hidden = YES;
            cell.progressLabel.hidden = YES;
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 5) {
        return 54;
    }
    return 44;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.docListModelS.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYRemarkImageCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:KRemarkImageCellID forIndexPath:indexPath];
    cell.deletedBtn.hidden = YES;
    TKYDocListModel *docListModel = self.docListModelS[indexPath.row];
    NSString *base64Str = docListModel.docPath;
    NSData *imageData = [[NSData alloc]initWithBase64EncodedString:base64Str options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    cell.imagePicV.image = [UIImage imageWithData:imageData];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYDocListModel *docListModel = self.docListModelS[indexPath.row];
    
    [ImageBrowserViewController show:self type:PhotoBroswerVCTypeModal index:indexPath.item imagesBlock:^NSArray *{
        
        return [self getAllImage];
    }];
}

- (NSMutableArray *)getAllImage{
    NSMutableArray *imageArr = [NSMutableArray array];
    for (TKYDocListModel *docListModel in self.docListModelS) {
        UIImage *image = [UIImage imageWithData:[[NSData alloc]initWithBase64EncodedString:docListModel.docPath options:(NSDataBase64DecodingIgnoreUnknownCharacters)]];
        [imageArr addObject:image];
    }
    return imageArr.copy;
}

#pragma mark--懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(kWidthScale *160, kWidthScale *160);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 2;
        //layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
        UICollectionView *collectionView;
        //if (IS_iPhoneX) {
            collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(kWidthScale *15,kHeightScale *30 , kScreenWidth, kHeightScale *160) collectionViewLayout:layout];
//      //  }else{
//            collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.addPicButtonView.kx_bottom, kScreenWidth, 130) collectionViewLayout:layout];
//        }
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.bounces = YES;
        //注册cell
        [collectionView registerNib:[UINib nibWithNibName:@"TKYRemarkImageCell" bundle:nil] forCellWithReuseIdentifier:KRemarkImageCellID];
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (UITableView *)tableV{
    if (_tableV == nil) {
        _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [_tableV registerNib:[UINib nibWithNibName:@"TKYProgressDetailCell" bundle:nil] forCellReuseIdentifier:KProgressDetailCellID];
        _tableV.tableFooterView = self.tableFootView;
    }
    return _tableV;
}

- (UIView *)tableFootView {
    if (_tableFootView == nil) {
        _tableFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeightScale *160)];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(30, kHeightScale*5, 80, kHeightScale *20)];
        lab.text = @"补充图片:";
        if (iPhone5SE ) {
            lab.font = [UIFont systemFontOfSize:15];
        }else{
            lab.font = [UIFont systemFontOfSize:14];
        }
        [_tableFootView addSubview:lab];
        [_tableFootView addSubview:self.collectionView];
    }
    return _tableFootView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
